//
//  LLSystemAlertView.m
//  TaoTuo
//
//  Created by admin on 2019/10/30.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLSystemAlertView.h"
#import <UIView+TYAlertView.h>

@interface LLSystemAlertView ()

@property (nonatomic, copy) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIView *hLineView;    // 横线
@property (nonatomic, strong) UIView *vLineView;    // 竖线
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;


@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *message;
@property (nonatomic, copy) NSString *cancelTitle;
@property (nonatomic, copy) NSString *confirmTitle;
@property (nonatomic, copy) dispatch_block_t cancelBlock;
@property (nonatomic, copy) BOOL(^confirmBlock)(void);

@end

@implementation LLSystemAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
    }
    return self;
}

// 取消
- (void)cancelBtnClick
{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [self hideView];
    [self dismissWithAnimationDuration:0];
}

// 确定
- (void)confirmBtnClick
{
    if (self.confirmBlock) {
        BOOL dismiss = self.confirmBlock();
        if (dismiss) {
            [self hideView];
            [self dismissWithAnimationDuration:0];
        }
    }
}

- (void)setupSubviews
{
    CGFloat titleH = self.title.length > 0 ? 50 : 0;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(titleH);
    }];
    
    CGFloat top = self.title.length > 0 ? 10 : 40;
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(top);
        make.left.mas_equalTo(10);
        make.right.equalTo(self).mas_offset(-10);
    }];
    
    CGFloat top2 = self.title.length > 0 ? 20 : 40;
    [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.messageLabel.mas_bottom).mas_offset(top2);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    CGFloat btnH = 45;
    [self.vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hLineView.mas_bottom);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(btnH);
    }];
    
    if (self.cancelTitle.length > 0) {
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.hLineView);
            make.left.equalTo(self);
            make.right.equalTo(self.mas_centerX);
            make.height.mas_equalTo(btnH);
        }];
        
        [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.cancelBtn.mas_right);
            make.right.equalTo(self);
            make.top.height.equalTo(self.cancelBtn);
        }];
    } else {
        [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.hLineView);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(btnH);
        }];
    }
    
    [self layoutIfNeeded];
    self.height = self.confirmBtn.bottom;
    
}

+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmBlock:(BOOL (^)(void))confirmBlock
{
    LLSystemAlertView *alertView = [self alertViewWithTitle:title message:message cancelTitle:@"" cancelBlock:nil confirmTitle:confirmTitle confirmBlock:confirmBlock];
    return alertView;
}

+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(dispatch_block_t)cancelBlock confirmTitle:(NSString *)confirmTitle confirmBlock:(BOOL (^)(void))confirmBlock
{
    LLSystemAlertView *alertView = [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 50, 100)];
    alertView.title = title;
    alertView.message = message;
    alertView.cancelTitle = cancelTitle;
    alertView.confirmTitle = confirmTitle;
    alertView.cancelBlock = cancelBlock;
    alertView.confirmBlock = confirmBlock;
    
    [alertView setupSubviews];
    
    return alertView;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;
}

- (void)setTitleBackgroundColor:(UIColor *)titleBackgroundColor
{
    _titleBackgroundColor = titleBackgroundColor;
    self.titleLabel.backgroundColor = titleBackgroundColor;
}

- (void)setCancleTitleColor:(UIColor *)cancleTitleColor
{
    _cancleTitleColor = cancleTitleColor;
    [self.cancelBtn setTitleColor:cancleTitleColor forState:UIControlStateNormal];
}

- (void)setConfirmTitleColor:(UIColor *)confirmTitleColor
{
    _confirmTitleColor = confirmTitleColor;
    [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
}

- (void)setCancleBtnBGColor:(UIColor *)cancleBtnBGColor
{
    _cancleBtnBGColor = cancleBtnBGColor;
    self.cancelBtn.backgroundColor = cancleBtnBGColor;
}

- (void)setConfirmBtnBGColor:(UIColor *)confirmBtnBGColor
{
    _confirmBtnBGColor = confirmBtnBGColor;
    self.confirmBtn.backgroundColor = confirmBtnBGColor;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelTextCenterWithTitle:self.title color:UIColorHex(333333) fontSize:18];
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}


- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel labelTextCenterWithTitle:@"" color:UIColorHex(565656) fontSize:16];
        _messageLabel.numberOfLines = 0;
        [self addSubview:_messageLabel];
        
        if (self.message.length > 0) {
            if ([self.message containsString:@"\\n"]) {
                self.message =[self.message stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
            }
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = 5;
            style.alignment = NSTextAlignmentCenter;
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message];
            [attr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, self.message.length)];
            _messageLabel.attributedText = attr;
        }
        
    }
    return _messageLabel;
}

- (UIView *)hLineView
{
    if (!_hLineView) {
        _hLineView = [[UIView alloc] init];
        _hLineView.backgroundColor = kSeparatorColor;
        [self addSubview:_hLineView];
    }
    return _hLineView;
}

- (UIView *)vLineView
{
    if (!_vLineView) {
        _vLineView = [[UIView alloc] init];
        _vLineView.backgroundColor = kSeparatorColor;
        [self addSubview:_vLineView];
    }
    return _vLineView;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton createButtonWithTitle:self.cancelTitle titleColor:UIColorHex(E74C3C) titleFont:[UIFont systemFontOfSize:16] backgroundColor:nil target:self action:@selector(cancelBtnClick)];
        [self addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton createButtonWithTitle:self.confirmTitle titleColor:kMainColor titleFont:[UIFont systemFontOfSize:16] backgroundColor:nil target:self action:@selector(confirmBtnClick)];
        [self addSubview:_confirmBtn];
    }
    return _confirmBtn;
}


@end
